package yxy.game.pm2.active.module.rank;

import org.joda.time.DateTime;
import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.acttaskrankreward;
import yxy.cherry.data.string.GameString;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.ActType;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.bean.module.rank.RankModule;
import yxy.game.pm2.bean.module.rank.active.RankRushEnum;
import yxy.game.pm2.bean.module.rank.active.RankRushHandle;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.module.mail.MailHandle;
import yxy.game.pm2.module.mail.MailTypeEnum;

import java.util.Collection;
import java.util.Map;

public class ActiveRushFutureHandle {

    private static final Logger logger = Logger.create(ActiveRushFutureHandle.class);

    // 时间控制===========================================================================

    public void update() {
        DateTime now = DateTime.now();

        for (AreaBean areaBean : AreaBean.all()) {

            activeControl sai = activeControl.get(areaBean.getAreaId(), ActType.冲榜);
            if (sai == null) {
                continue;
            }

            ActiveRushModule module = ActiveRushModule.get(areaBean.getAreaId());
            if (module == null) {
                continue;
            }

            // 已经领取奖励了
            if (module.isAward()) {
                continue;
            }

            int type = module.getRewardtype();

            // 不是一天 发日结算奖励
            lssueReward(module, areaBean, type, now);
//			ThreadPoolManager.getInstance().execute(new Runnable() {
//
//				@Override
//				public void run() {
//					lssueReward(module, areaBean, type, now);
//				}
//			});

//			// 活动结束 发放奖励
//			if (ActiveUtil.days(module.getRewardtime(), now) >= 1 && module.isRunning()) {
//				System.err.println(areaBean.getAreaId() + "区冲榜" + type + "发排名奖励了！！！！" + DateTime.now().toString());
//
//				Map<String, acttaskrankreward> all = ActDataCenter.getAllData(acttaskrankreward.class,
//						areaBean.getAreaId());
//
//				for (acttaskrankreward rankdata : all.values()) {
//					Collection<RankEntry> rankEntries = RankRushHandle.range(areaBean.getAreaId(), type,
//							rankdata.getRankingmin(), rankdata.getRankingmax());
//
//					Collection<Currency> rewards = Currency.create(rankdata.getRewardtype(), rankdata.getRewardid(),
//							rankdata.getRewardnum());
//					int rank = rankdata.getRankingmin();
//
//					for (RankEntry entry : rankEntries) {
//						String playerCode = entry.getKey();
//
//						// 发邮件
//						String title = getTitle(type);
//						String content = getMsg(type, rank);
//						new MailHandle().sendMail(playerCode, MailTypeEnum.Mail, title, content, CCReason.活动_冲榜,
//								rewards);
//						rank++;
//					}
//
//				}
//
//				module.setAward(true);
//				module.set();
//
//				System.err.println(areaBean.getAreaId() + "区冲榜" + type + "排名奖励了发完了！！！！" + DateTime.now().toString());
//			}
//			}

        }
    }

    private void lssueReward(ActiveRushModule module, AreaBean areaBean, int type, DateTime now) {
        // 活动结束 发放奖励
        if (!module.isAward() && ActiveUtil.days(module.getRewardtime(), now) >= 1 && module.isRunning()) {
            logger.info(areaBean.getAreaId() + "区冲榜" + type + "发排名奖励了！！！！" + DateTime.now().toString());
//			System.err.println(areaBean.getAreaId() + "区冲榜" + type + "发排名奖励了！！！！" + DateTime.now().toString());

            module.setAward(true);
            module.set();

            int rank = 1;
            int size = 0;
            int score = 0;
            Map<String, acttaskrankreward> all = ActDataCenter.getAllData(acttaskrankreward.class, areaBean.getAreaId());
            RankModule rankRush = RankRushHandle.rank(areaBean.getAreaId(), type);
            Collection<RankEntry> allRank = rankRush.allFlashback();
            for (RankEntry entry : allRank) {
                String pCode = entry.getKey();
                int value = entry.getValue().intValue();
                size++;
                //不同分排名变动
                if (value != score && size > 1) {
                    rank = size;
                }
                score = value;

                //获取奖励信息，并进行发奖励
                acttaskrankreward data = null;
                for (yxy.cherry.data.bean.acttaskrankreward acttaskrankreward : all.values()) {
                    if (acttaskrankreward.getRankingmin() <= rank && acttaskrankreward.getRankingmax() >= rank){
                        data = acttaskrankreward;
                    }
                }

                //发邮件
                if(data!=null){
                    Collection<Currency> rewards = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
                    String title = getTitle(type);
                    String content = getMsg(type, rank);
                    new MailHandle().sendMail(pCode, MailTypeEnum.Mail, title, content, CCReason.活动_冲榜, rewards);
                }
            }

//			System.err.println(areaBean.getAreaId() + "区冲榜" + type + "排名奖励了发完了！！！！" + DateTime.now().toString());
            logger.info(areaBean.getAreaId() + "区冲榜" + type + "排名奖励了发完了！！！！" + DateTime.now().toString());
        }
    }

    private String getTitle(int type) {
        if (type == RankRushEnum.冲榜_剿匪次数.getType()) {
            return GameString.冲榜活动剿匪排行奖励奖_title().getText();
        }

        if (type == RankRushEnum.冲榜_快速作战.getType()) {
            return GameString.冲榜活动扫荡排行奖励奖_title().getText();
        }

        if (type == RankRushEnum.冲榜_金币兑换.getType()) {
            return GameString.冲榜活动金币排行奖励奖_title().getText();
        }

        if (type == RankRushEnum.冲榜_副本关卡.getType()) {
            return GameString.冲榜活动副本排行奖励奖_title().getText();
        }
        return "";
    }

    private String getMsg(int type, int rank) {
        if (type == RankRushEnum.冲榜_剿匪次数.getType()) {
            return GameString.冲榜活动剿匪排行奖励奖_msg(rank).getText();
        }

        if (type == RankRushEnum.冲榜_快速作战.getType()) {
            return GameString.冲榜活动扫荡排行奖励奖_msg(rank).getText();
        }

        if (type == RankRushEnum.冲榜_金币兑换.getType()) {
            return GameString.冲榜活动金币排行奖励奖_msg(rank).getText();
        }

        if (type == RankRushEnum.冲榜_副本关卡.getType()) {
            return GameString.冲榜活动副本排行奖励奖_msg(rank).getText();
        }
        return "";
    }

    // XXX TEST 短时测试
    static private boolean isOpen(DateTime time) {
        if (time == null) {
            return false;
        }

        int minute = time.getMinuteOfHour();
        return minute >= 5 && minute <= 59;// 每小时5分开始
    }


}
